package com.erp.erp_servers.Quoted_Info.imp;

import com.erp.enums.OperationStatusEnum;
import com.erp.enums.SequenceEnum;
import com.erp.erp_dao.Quoted_Info.QuotedInfoDao;
import com.erp.erp_dao.Quoted_Info.QuotedProductCostInfoDao;
import com.erp.erp_dao.Quoted_Info.QuotedProductInfoDao;
import com.erp.erp_dao.Quoted_Info.QuotedProductPartsInfoDAO;
import com.erp.erp_dao.nextId.NextIdDao;
import com.erp.erp_entitys.Quoted_Info.QuotedInfo;
import com.erp.erp_entitys.Quoted_Info.QuotedProductCostInfo;
import com.erp.erp_entitys.Quoted_Info.QuotedProductInfo;
import com.erp.erp_entitys.Quoted_Info.QuotedProductPartsInfo;
import com.erp.erp_servers.Quoted_Info.QuotedInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 报价表(QuotedInfo)表服务实现类
 *
 * @author makejava
 * @since 2020-10-11 17:22:15
 */
@Service("quotedInfoService")
public class QuotedInfoServiceImpl implements QuotedInfoService {
    @Resource
    private QuotedInfoDao quotedInfoDao;

    @Resource
    private QuotedProductInfoDao productInfoDao;

    @Resource
    private QuotedProductCostInfoDao costInfoDao;

    @Resource
    private QuotedProductPartsInfoDAO partsInfoDAO;


    @Resource
    private NextIdDao nextIdDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public QuotedInfo queryById(Long id) {
        return this.quotedInfoDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<QuotedInfo> queryAllByLimit(int offset, int limit) {
        return this.quotedInfoDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param quotedInfo 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public QuotedInfo insert(QuotedInfo quotedInfo) {
        quotedInfo.setId(nextIdDao.selectNextId(SequenceEnum.QUOTEDINFO.getValue()));
        quotedInfo.setStatus(String.valueOf(OperationStatusEnum.DRAFT_BOX.getId()));
        quotedInfo.setCreatetime(new Date());
        quotedInfo.setEdittime(new Date());
        quotedInfo.getProductInfos().forEach(product ->{
            product.setQuotedid(quotedInfo.getId());
            this.productInfoDao.insert(product);
        });
        quotedInfo.getCosts().forEach(cost ->{
            cost.setQuotedid(quotedInfo.getId());
            this.costInfoDao.insert(cost);
        });
        quotedInfo.getPartsInfos().forEach(parts ->{
            parts.setQuotedid(quotedInfo.getId());
            this.partsInfoDAO.insert(parts);
        });
        this.quotedInfoDao.insertSelective(quotedInfo);
        return quotedInfo;
    }

    /**
     * 修改数据
     *
     * @param quotedInfo 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public int update(QuotedInfo quotedInfo) {
        quotedInfo.setEdittime(new Date());
        quotedInfo.getProductInfos().forEach(cost ->{
            if(cost.getProductid() != null){
                this.productInfoDao.update(cost);
            }else {
                cost.setQuotedid(quotedInfo.getId());
                this.productInfoDao.insert(cost);
            }
        });
        quotedInfo.getCosts().forEach(cost ->{
            if(cost.getId() != null){
                this.costInfoDao.update(cost);
            }else {
                cost.setQuotedid(quotedInfo.getId());
                this.costInfoDao.insert(cost);
            }
        });
        quotedInfo.getPartsInfos().forEach(parts ->{
            if(parts.getPartsid() != null){
                this.partsInfoDAO.update(parts);
            }else {
                parts.setQuotedid(quotedInfo.getId());
                this.partsInfoDAO.insert(parts);
            }
        });
        return this.quotedInfoDao.update(quotedInfo);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean deleteById(Long id) {
        // 判断状态是否能删除
//        QuotedInfo quotedInfo = this.queryById(id);
//        quotedInfo.getStatus()
        this.productInfoDao.deleteByQuotedId(id);
        this.costInfoDao.deleteByQuotedId(id);
        this.partsInfoDAO.deleteByQuotedId(id);
        return this.quotedInfoDao.deleteById(id) > 0;
    }

    @Override
    public PageInfo<QuotedInfo> selectByPage(QuotedInfo record) {
        List<QuotedInfo> quotedInfos = quotedInfoDao.queryAll(record);
        PageInfo pageInfo = new PageInfo(quotedInfos);
        pageInfo.setList(quotedInfos);
        return pageInfo;
    }

    @Override
    public PageInfo<QuotedProductPartsInfo> selectPartsByPage(QuotedProductPartsInfo record) {
        List<QuotedProductPartsInfo> quotedInfos = partsInfoDAO.queryAll(record);
        PageInfo pageInfo = new PageInfo(quotedInfos);
        pageInfo.setList(quotedInfos);
        return pageInfo;
    }

    @Override
    public PageInfo<QuotedProductCostInfo> selectCostByPage(QuotedProductCostInfo record) {
        List<QuotedProductCostInfo> quotedInfos = costInfoDao.queryAll(record);
        PageInfo pageInfo = new PageInfo(quotedInfos);
        pageInfo.setList(quotedInfos);
        return pageInfo;
    }

    @Override
    public PageInfo<QuotedProductInfo> selectProductsByPage(QuotedProductInfo record) {
        List<QuotedProductInfo> quotedInfos = productInfoDao.queryAll(record);
        PageInfo pageInfo = new PageInfo(quotedInfos);
        pageInfo.setList(quotedInfos);
        return pageInfo;
    }

    @Override
    @Transactional
    public Boolean deleteProduct(Long id) {
        QuotedProductInfo productInfo = productInfoDao.queryById(id);
        partsInfoDAO.deleteByGoodsId(productInfo.getQuotedproductid());
        return this.productInfoDao.deleteById(id) > 0;
    }

    @Override
    public Boolean deletePart(Long id) {
        return this.partsInfoDAO.deleteById(id) > 0;
    }

    @Override
    public Boolean deleteCost(Long id) {
        return this.costInfoDao.deleteById(id) > 0;
    }
}